const inquirer = require('inquirer'); //命令行交互
const { buildCheckbox } = require('../config.js');
module.exports = async () => {
      inquirer.prompt(buildCheckbox).then(result => { //inquirer 交互问答
        const packages = result.Packages
        const exec = require('child_process').exec;
        if(packages.length>0){
            packages.some(package => {
                const openCmd = ' start cmd \/k  '+'\"npm run serve-'+ package+'\"' ;
                exec(openCmd, (err, stdout, stderr) => {
                    console.log(err, stdout, stderr, 'err, stdout, stderr===')
                    if (err){
                        console.log(err);
                        console.warn(package,' 启动服务命令执行失败');
                    } else {
                        console.log(stdout);
                        console.warn(package,' 成功');
                    }
                });
            })
        }
        // const cmdStr = 'node ./node_modules/@vue/cli-service/bin/vue-cli-service.js  serve --dir '+package+' --build '+package
        // new Promise(async () => { // 启动 server.js
        //     const nodemon = require(`nodemon`)
        //     nodemon({
        //       ignoreRoot: [],
        //       exec: cmdStr,
        //       watch: [`./node_modules/@vue/cli-service/bin/vue-cli-service.js`],
        //       stdout: false,
        //     //   st
        //     })
        //     .on('readable', function(arg) { // the `readable` event indicates that data is ready to pick up
        //       this.stdout.pipe(process.stdout) // 把子进程的输出定向到本进入输出
        //       this.stderr.pipe(process.stderr) // 错误输出, 例如按需安装依赖时无权限
        //       this.stdout.on(`data`, data => {
        //         //   console.log(data, '==data==')
        //       })
        //     })
        //   })
        // exec(cmdStr, (err, stdout, stderr) => {
        //     stdout.pipe(process.stdout)

        //     console.log(err, stdout, stderr, 'err, stdout, stderr==')
        //     if (err){
        //         console.log(err);
        //         console.warn(package,' serve运行命令执行失败');
        //     } else {
        //         console.log(stdout);
        //         console.warn(package,' serve运行命令执行成功');
        //     }
        // });
      })
 }